<?php
namespace app\admin\controller;

use \frame\libs\Session;
use \frame\libs\BaseController;
use \app\admin\model\UserModel;

/**
 * 登录
 */
class UserController extends BaseController
{  
    /**
     * 用户管理页
     * @return [type] [description]
     */
    public function index()
    {
        $this->checkPage();
        include VIEW_PATH . 'user/index.html';
    }

    /**
     * 获取用户数
     * @return [type] [description]
     */
    public function getUserNum()
    {
        echo json_encode(UserModel::getInstance()->getUserNum());
        return true;
    }

    /**
     * 获取当天登录的用户数
     * @return [type] [description]
     */
    public function getUserNumHasLoginToday()
    {
        echo json_encode(UserModel::getInstance()->getUserNumHasLoginToday());
        return true;
    }

    /**
     * 获取所有用户信息和角色
     * @return [type] [description]
     */
    public function getUsersWithRole()
    {
        echo json_encode(UserModel::getInstance()->getUsersWithRole());
        return true;
    }

    /**
     * 获取用户登录次数
     * @return [type] [description]
     */
    public function getUserLoginNum()
    {
        echo json_encode(UserModel::getInstance()->getUserLoginNum());
        return true;
    }

    /**
     * 更新启用状态
     * @return bool [description]
     */
    public function updateStatus()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = $_POST;
        $data['update_time'] = time();
        if (UserModel::getInstance()->updateStatus($data)) {
            echo $this->success(null, "设置成功！");
            return true;
        }else{
            echo $this->error(null, "设置失败！");
            return false;
        }
    }

    /**
     * 删除用户
     * @return [type] [description]
     */
    public function delete()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        if (UserModel::getInstance()->deleteUserById($_POST['uid'])) {
            echo $this->success(null, "删除成功！");
            return true;
        }else{
            echo $this->error(null, "删除失败！");
            return false;
        }
    }

    /**
     * 添加用户
     * @return bool [description]
     */
    public function insert()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $user = UserModel::getInstance()->getUserByUsername($_POST['username']);
        if ($user) {
            echo $this->error(null, "该用户名已存在！");
            return false;
        }
        $data = $_POST;
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['password'] = md5($_POST['password']);
        if (UserModel::getInstance()->insertUser($data)) {
            echo $this->success(null, "添加成功！");
            return true;
        }else{
            echo $this->error(null, "添加失败！");
            return false;
        }
    }

    /**
     * 更新用户
     * @return bool [description]
     */
    public function update()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = $_POST;
        $data['update_time'] = time();
        if (UserModel::getInstance()->updateUser($data)) {
            echo $this->success(null, "编辑成功！");
            return true;
        }else{
            echo $this->error(null, "编辑失败！");
            return false;
        }
    }

    /**
     * 更新密码
     * @return bool [description]
     */
    public function updatePassword()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = array();
        if (isset($_POST['uid']) && !empty($_POST['uid'])) {
            $data['uid'] = $_POST['uid'];
        }else{
            echo $this->error();
            return false;
        }
        if ($_POST['new_password'] != $_POST['again_password']) {
            echo $this->error(null, "两次输入新密码不一致！");
            return false;
        }
        $user = UserModel::getInstance()->getUserByUid($data['uid']);
        if (!$user) {
            echo $this->error(null, "用户不存在！");
            return false;
        }
        if ($user['password'] != md5($_POST['old_password'])) {
            echo $this->error(null, "密码错误！");
            return false;
        }
        $data['password'] = md5($_POST['new_password']);
        $data['update_time'] = time();
        if (UserModel::getInstance()->updatePassword($data)) {
            echo $this->success(null, "设置密码成功！");
            return true;
        }else{
            echo $this->error(null, "设置密码失败！");
            return false;
        }
    }

    /**
     * 用户自行更新密码
     * @return bool [description]
     */
    public function updatePasswordBySelf()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = array();
        $data['uid'] = Session::get('uid');
        if ($_POST['new_password'] != $_POST['again_password']) {
            echo $this->error(null, "两次输入新密码不一致！");
            return false;
        }
        $user = UserModel::getInstance()->getUserByUid($data['uid']);
        if (!$user) {
            echo $this->error(null, "用户不存在！");
            return false;
        }
        if ($user['password'] != md5($_POST['old_password'])) {
            echo $this->error(null, "密码错误！");
            return false;
        }
        $data['password'] = md5($_POST['new_password']);
        $data['update_time'] = time();
        if (UserModel::getInstance()->updatePassword($data)) {
            echo $this->success(null, "设置密码成功！");
            return true;
        }else{
            echo $this->error(null, "设置密码失败！");
            return false;
        }
    }

    /**
     * 根据id获取用户
     * @return [type] [description]
     */
    public function getUserByUid()
    {
        if (!isset($_POST['uid']) || empty($_POST['uid'])) {
            echo $this->error();
            return false;
        }
        if ($user = UserModel::getInstance()->getUserByUid($_POST['uid'])) {
            echo $this->success($user);
            return true;
        }else{
            echo $this->error();
            return false;
        }
    }

    public function getUserBySessionUid()
    {
        if ($user = UserModel::getInstance()->getUserByUid(Session::get('uid'))) {
            echo $this->success($user);
            return true;
        }else{
            echo $this->error();
            return false;
        }
    }

    /**
     * 更新用户基本信息, 导航栏用户基本信息自修改
     * @return [type] [description]
     */
    public function updateUserInfo()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = array();
        $data['uid'] = Session::get('uid');
        $user = UserModel::getInstance()->getUserByUsername($_POST['username']);
        if ($user && $user['username'] == $_POST['username']) {
            echo $this->error(null, "该用户名已被使用!");
            return false;
        }else{
            $data['username'] = $_POST['username'];
        }
        $data['name_zh'] = $_POST['name_zh'];
        $data['phone'] = $_POST['phone'];
        if (UserModel::getInstance()->updateUserInfo($data)) {
            echo $this->success(null, "修改成功！");
            return true;
        }else{
            echo $this->error(null, "修改失败！");
            return false;
        }
    }

}